/**
 * Vitals
 * This class represents a patient's recorded vitals
 * Parent class of Height, Weight, BodyTemp, BloodPressure
 */
package docoffice.doss;
import java.util.logging.Logger;
import java.sql.ResultSet;

import docoffice.database.DatabaseManager;

/**
 * @author George Chi
 * @version $Revision: 1.8 $
 */
public class Vitals {
	
	/**
	 * Field vitPulse.
	 * Field id.
	 */
	public int vitPulse, id;

	/**
	 * Field vitHeight.
	 */
	public Height vitHeight;

	/**
	 * Field vitBodyTemp.
	 */
	public BodyTemp vitBodyTemp;

	/**
	 * Field vitWeight.
	 */
	public Weight vitWeight;

	/**
	 * Field vitBloodPres.
	 */
	public BloodPressure vitBloodPres;
	
	/**
	 * Field LOG.
	 */
	private static final Logger LOG = Logger.getLogger("Vitals");

	/**
	 * Constructor for Vitals.
	 * @param patientPulse int
	 * @param height Height
	 * @param bodyTemp BodyTemp
	 * @param weight Weight
	 * @param bloodPres BloodPressure
	 */
	public Vitals(int patientPulse, Height height, BodyTemp bodyTemp, 
			Weight weight, BloodPressure bloodPres) {
		setVitPulse(patientPulse);
		setVitHeight(height);
		setVitBodyTemp(bodyTemp);
		setVitWeight(weight);
		setVitBloodPres(bloodPres);
	}

	/**
	 * Constructor for Vitals.
	 * @param patientPulse int
	 */
	public Vitals(int patientPulse) {
		this.vitPulse = patientPulse;
	}

	/**
	 * Method getVitPulse.
	
	 * @return int */
	public int getVitPulse() {
		return vitPulse;
	}

	/**
	 * Method setVitPulse.
	 * @param vitPulse int
	 */
	public final void setVitPulse(int vitPulse) {
		this.vitPulse = vitPulse;
	}

	/**
	 * Method getId.
	
	 * @return int */
	public int getId(){
		return this.id;
	}
	
	/**
	 * Method setId.
	 * @param id int
	 */
	public void setId(int id){
		this.id = id;
	}
	
	/**
	 * Method getVitHeight.
	
	 * @return Vitals */
	public Height getVitHeight() {
		return vitHeight;
	}

	/**
	 * Method setVitHeight.
	 * @param vitHeight Vitals
	 */
	public final void setVitHeight(Height vitHeight) {
		this.vitHeight = vitHeight;
	}

	/**
	 * Method getVitBodyTemp.
	
	 * @return Vitals */
	public BodyTemp getVitBodyTemp() {
		return vitBodyTemp;
	}

	/**
	 * Method setVitBodyTemp.
	 * @param vitBodyTemp Vitals
	 */
	public final void setVitBodyTemp(BodyTemp vitBodyTemp) {
		this.vitBodyTemp = vitBodyTemp;
	}

	/**
	 * Method getVitWeight.
	
	 * @return Vitals */
	public Weight getVitWeight() {
		return vitWeight;
	}

	/**
	 * Method setVitWeight.
	 * @param vitWeight Vitals
	 */
	public final void setVitWeight(Weight vitWeight) {
		this.vitWeight = vitWeight;
	}

	/**
	 * Method getVitBloodPres.
	
	 * @return Vitals */
	public BloodPressure getVitBloodPres() {
		return vitBloodPres;
	}

	/**
	 * Method setVitBloodPres.
	 * @param vitBloodPres Vitals
	 */
	public final void setVitBloodPres(BloodPressure vitBloodPres) {
		this.vitBloodPres = vitBloodPres;
	}
	
	/**
	 * Method createVitals.
	 * @param newPulse int
	 * @param newHeight Height
	 * @param newTemp BodyTemp
	 * @param newWeight Weight
	 * @param newPres BloodPressure
	 * @return Vitals */
	public void createVitals(int newPulse, Height newHeight, 
			BodyTemp newTemp, Weight newWeight, BloodPressure newPres) {
		
		final int feet = newHeight.getHeightFeet();
		final int inches = newHeight.getHeightInches();
		final double fahrenheit = newTemp.getBodyTempFahr();
		final double lbs = newWeight.getWeightPounds();
		
		final DatabaseManager dbMgr = new DatabaseManager();
		final String sqlQuery = "INSERT INTO vitals (Pulse, SystolicBloodPressure, " +
				"DiastolicBloodPressure, Weight, HeightFeet, BodyTemp, HeightInches) VALUES (" + 
				newPulse + ", " + newPres.systolic + ", " + newPres.diastolic + ", " + 
				lbs + ", " + feet + ", " + fahrenheit + ", " + inches + ")";

		try{
			dbMgr.runUpdateQuery(sqlQuery);
		} catch(Exception e){
			LOG.severe("A SQL Error occurred while creating new vital signs");
			e.printStackTrace();
		} finally{
			dbMgr.closeConnection();
		}
		
		LOG.info("Vital Signs entered correctly");
	}
	
	/**
	 * Method readVitals.
	 * @param vitals Vitals
	
	 * @return Vitals */
	public Vitals readVitals(Vitals vitals) {
		final int id = vitals.getId();
		final DatabaseManager dbMgr = new DatabaseManager();
		final String sqlQuery = "SELECT * FROM vitals WHERE VitalsID = " + id;
		ResultSet rs = null;
		
		try{
			rs = dbMgr.runReadQuery(sqlQuery);
			rs.first();
			final int pulse = rs.getInt("Pulse");
			final int systolic = rs.getInt("SystolicBloodPressure");
			final int diastolic = rs.getInt("DiastolicBloodPressure");
			final int heightFt = rs.getInt("HeightFeet");
			final int heightIn = rs.getInt("HeightInches");
			final int weight = rs.getInt("Weight");
			final double bodytemp = rs.getDouble("BodyTemp");
			
			final BloodPressure bp = new BloodPressure(pulse, systolic, diastolic);
			final Height ht = new Height(pulse, heightFt, heightIn);
			final Weight wt = new Weight(pulse, weight);
			final BodyTemp bt = new BodyTemp(pulse, bodytemp);
			
			vitals.setVitPulse(pulse);
			vitals.setVitBloodPres(bp);
			vitals.setVitBodyTemp(bt);
			vitals.setVitHeight(ht);
			vitals.setVitWeight(wt);
		} catch(Exception e){
			LOG.severe("A SQL Error has occurred while creating new vitals");
			e.printStackTrace();
		} finally{
			dbMgr.closeConnection(rs);
		}
		
		LOG.info("Vitals created correctly");
		return vitals;
	}
	
	public int readVitalsID(Vitals vitals){
		int id = 0;
		final int pulse = vitals.getVitPulse();
		final BloodPressure bp = vitals.getVitBloodPres();
		final int systolic = bp.getSystolic();
		final int diastolic = bp.getDiastolic();
		final Weight weight = vitals.getVitWeight();
		final double lbs = weight.getWeightPounds();
		final Height height = vitals.getVitHeight();
		final int feet = height.getHeightFeet();
		final int inches = height.getHeightInches();
		final BodyTemp temperature = vitals.getVitBodyTemp();
		final double fahrenheit = temperature.getBodyTempFahr();
		
		final DatabaseManager dbMgr = new DatabaseManager();
		final String sqlQuery = "SELECT VitalsID FROM vitals WHERE Pulse = "
			+ pulse + " AND SystolicBloodPressure = " + systolic + " AND DiastolicBloodPressure = "
			+ diastolic + " AND Weight = " + lbs + " AND HeightFeet = " + feet + " AND BodyTemp = "
			+ fahrenheit + " AND HeightInches = " + inches;
		ResultSet rs = null;
		
		try{
			rs = dbMgr.runReadQuery(sqlQuery);
			rs.first();
			id = rs.getInt("VitalsID");
		} catch(Exception e){
			LOG.severe("A SQL Error has occurred while creating new vitals");
			e.printStackTrace();
		} finally{
			dbMgr.closeConnection(rs);
		}
		
		LOG.info("VitalsID read correctly");
		return id;
	}
	
	/**
	 * Method updateVitals.
	 * @param vitals Vitals
	 */
	public void updateVitals(Vitals vitals) {
		final int id = vitals.getId();
		final int pulse = vitals.getVitPulse();
		final int systolic = vitals.vitBloodPres.getSystolic();
		final int diastolic = vitals.vitBloodPres.getDiastolic();
		final int heightFt = vitals.vitHeight.getHeightFeet();
		final int heightIn = vitals.vitHeight.getHeightInches();
		final double weight = vitals.vitWeight.getWeightPounds();
		final double bodytemp = vitals.vitBodyTemp.getBodyTempFahr();
		
		final DatabaseManager dbMgr = new DatabaseManager();
		final String sqlQuery = "UPDATE vitals SET Pulse = " + pulse + 
		        ", SystolicBloodPressure = " + systolic + ", DiastolicBloodPressure = " + 
		        diastolic + ", Weight = " + weight + ", HeightFeet = " + heightFt + 
		        ", HeightInches = " + heightIn + ", BodyTemp = " + bodytemp + 
		        " WHERE VitalsID = " + id;
		
		try{
			dbMgr.runUpdateQuery(sqlQuery);
		} catch(Exception e){
			LOG.severe("A SQL Error occurred while updating vitals info");
			e.printStackTrace();
		} finally{
			dbMgr.closeConnection();
		}
		
		LOG.info("Vitals info updated successfully");
	}
	
	/**
	 * Method deleteVitals.
	 * @param vitals Vitals
	 */
	public void deleteVitals(Vitals vitals) {
		final int id = vitals.getId();
		final DatabaseManager dbMgr = new DatabaseManager();
		final String sqlQuery = "DELETE FROM vitals WHERE VitalsID = " + id;
		
		try{
			dbMgr.runUpdateQuery(sqlQuery);
		} catch(Exception e){
			LOG.severe("A SQL Error occurred while deleting vitals info");
			e.printStackTrace();
		} finally{
			dbMgr.closeConnection();
		}
		LOG.info("Vitals deleted successfully");
	}
	
	/**
	 * Method toString.
	
	 * @return String */
	public String toString() {
		return "Vitals will return a patient's vitals information.";
	}
	
	
}
